<?php
/*
+---------------------------------------------------------------------------
|   Arka-Tech Web Application v2.0.0
|   ========================================
|   by Christopher Hall
|   (c) 2009 ArkahnX-Technology
|   http://arka-tech.net
|   ========================================
|   Web: http://arka-tech.net/os/
|   Time: Teus, 14 July 2009 6:51 PM
|   Release: RmVicnVhcnkgMjAwOSBMYXVuY2ggNA==
|   Email: ArkahnX@gmail.com
|   Licence Info: http://www.arka-tech.net/os/
+---------------------------------------------------------------------------
*/

include_once('./CORE.inc.php');
if (check_versions(ATWA_VERSION,'1.2.0.0') == 2) {
	output_errors(array(lang_translate('installer-update-error-under15','This updater is only for ATWA version 1.2 and higher. You are using ATWA [version1]. Please update to ATWA 1.2 before updating to ATWA [version2].',array('[version1]' => ATWA_VERSION,'[version2]' => INSTALL_VERSION))));
} elseif ($_REQUEST['update']) {
	update_do($_REQUEST['update']);
} else {
	if (TYPE_SYSTEM) {
		$resetsystem = '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <form action="./index.php?back=1" method="post"><input class="iButton" type="submit" value="' . lang_translate('installer-system-back','Back to menu') . '" /></form>';
	}
	output_do('<div class="button"><form action="./index.php?update=1" method="post"><input class="iButton" type="submit" value="' . lang_translate('installer-update-do','Update ATWA!') . '" /></form>' . $resetsystem . '</div>',0,lang_translate('installer-update-title','Please continue to update your ATWA installation to version [version].',array('[version]' => INSTALL_VERSION)));
}

function update_do($update) {
	// Load update.xml
	$xmlUpdate = xml_xml2array(file_get_contents(INSTALL_DIR . 'files/update.xml'));
	
	// Untar source.tar.gz
	if ($update != '2') {
		if (TYPE_UPDATE === 1 || TYPE_SYSTEM === 1) {
			include_once(INSTALL_DIR . 'pcl-tar.php');
			if (!PclTarExtract(INSTALL_PACKAGE,'./','','tgz')) {
				output_errors(array(lang_translate('installer-update-error-extract','The update files could not be extracted. To update ATWA manually, extract the contents into the same directory.',array('[package]' => INSTALL_PACKAGE))));
			}
		}
		
		// Delete files and folders
		foreach ($xmlUpdate['Package'][0]['delete'][0] as $key => $value) {
			if (check_versions(ATWA_VERSION,$key) < 2) {
				foreach ($value as $file) {
					path_delete('./ATWA/' . $file);
				}
			}
		}
		foreach ($xmlUpdate['Installer'][0]['files'][0]['delete'] as $file) {
			$file = str_replace('%apps%','./' . REAL_OS_ROOT . '/' . APP_DIR,$file);
			$file = str_replace('%extern%','./' . REAL_OS_ROOT . '/' . EXTERN_DIR,$file);
			$file = str_replace('%system%','./' . REAL_OS_ROOT . '/' . SYSTEM_DIR,$file);
			$file = str_replace('%themes%','./' . REAL_OS_ROOT . '/' . THEMES_DIR,$file);
			path_delete($file);
		}
		
		// Rename dir
		if (!path_move('./ATWA/','./' . REAL_OS_ROOT . '/')) {
			output_errors(array(lang_translate('installer-update-error-renaming','Could not rename the directory "./ATWA/". Please, rename it manually to "[hash]" and overwrite all existing files.',array('[hash]' => REAL_OS_ROOT))),array('./index.php?update=2',lang_translate('installer-update-continue','Continue...')),0);
		}
	}
	
	// Update module files
	$xmlModules = xml_xml2array(file_get_contents(INSTALL_DIR . 'files/modules.xml'));
	foreach ($xmlModules['modules'][0] as $module => $files) {
		@mkdir('./' . $module . '/');
		foreach ($files[0] as $file => $content) {
			file_put_contents('./' . $module . '/' . $file,base64_decode($content[0]));
		}
	}
	
	// Update the system configuration files
	foreach ($xmlUpdate['Installer'][0]['conf'] as $array) {
		foreach ($array as $conf => $array2) {
			$conf_xml = xml_xml2array(file_get_contents('./' . REAL_OS_ROOT . '/' . SYSTEM_DIR . '/' . SYSTEM_CONF_DIR . '/' . $conf . '.xml'));
			foreach ($array2[0] as $action => $object) {
				if ($action == 'delete') {
					foreach ($object as $key) {
						unset($conf_xml['CONFIG'][0][$key]);
					}
				} elseif ($action == 'add') {
					foreach ($object[0] as $key => $value) {
						if (!$conf_xml['CONFIG'][0][$key]) {
							$conf_xml['CONFIG'][0][$key] = $value;
						}
					}
				} else {
					foreach ($object[0] as $key => $value) {
						$conf_xml['CONFIG'][0][$key] = $value;
					}
				}
			}
			file_put_contents('./' . REAL_OS_ROOT . '/' . SYSTEM_DIR . '/' . SYSTEM_CONF_DIR . '/' . $conf . '.xml',xml_array2xml($conf_xml));
		}
	}
	
	// Update CACHE_VERSION
	$xmlSystem = xml_xml2array(file_get_contents('./' . REAL_OS_ROOT . '/' . SYSTEM_DIR . '/' . SYSTEM_CONF_DIR . '/system.xml'));
	$xmlSystem['CONFIG'][0]['CACHE_VERSION'][0] += 1;
	if (ATWA_VERSION == '1.8.5.0' && $xmlSystem['CONFIG'][0]['ATWA_DEBUG_MODE'][0] == 3 && $xmlSystem['CONFIG'][0]['EXTERN_CACHE_AUTO'][0] == 0) {
		$xmlSystem['CONFIG'][0]['ATWA_DEBUG_MODE'][0] = 0;
		$xmlSystem['CONFIG'][0]['EXTERN_CACHE_AUTO'][0] = 2;
	}
	file_put_contents('./' . REAL_OS_ROOT . '/' . SYSTEM_DIR . '/' . SYSTEM_CONF_DIR . '/system.xml',xml_array2xml($xmlSystem));
	
	// Version specific update functions
	$path = INSTALL_DIR . 'modules/update/';
	if ($handler = @opendir($path)) {
		while (false !== ($file = readdir($handler))) {
			if (substr($file,-8) == '.php') {
				$list[] = basename($file,'.php');
			}
		}
		closedir($handler);
	}
	uasort($list,'check_version_uasort');
	foreach ($list as $entry) {
		if (check_versions($entry,ATWA_VERSION) < 2) {
			include(INSTALL_DIR . 'modules/update/' . $entry . '.php');
		}
	}
	
	// index.php
	copy(INSTALL_DIR . 'files/index.txt','./index.php');
	// settings.php
	$settingsphp = file_get_contents(INSTALL_DIR . 'files/settings.txt');
	foreach ($xmlUpdate['Updater'][0]['settings'][0] as $key => $value) {
		if (defined($key)) {
			$settingsphp = str_replace("define('" . $key . "','" . $value[0] . "');","define('" . $key . "','" . constant($key) . "');",$settingsphp);
		}
	}
	file_put_contents('./settings.php',$settingsphp);
	
	// Remove the installer
	output_remove();
}
?>